package com.qf.springcloud.item.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qf.springcloud.item.pojo.Item;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

@RestController
@RequestMapping("/items")
@RefreshScope//当配置变化的时候会刷新配置
public class ItemController {

    private String username ="dadasd";


    private int count;
    @GetMapping("/item/{id}")
    @HystrixCommand(fallbackMethod = "suibianxieyigemingzi")//当当前方法出现异常的时候会执行我们指定的方法
    public Item getItemById(@PathVariable Integer id, HttpServletRequest request) {
        System.err.println(request.getRemoteAddr());
        count++;
        System.err.println(count);
        Item item = new Item();
        item.setId(id);
        item.setCategory("计生用品");
        item.setName("打气筒");
        item.setPrice(10000);
        if (new Random().nextBoolean()) {
            int i = 1 / 0;
        }
        return item;
    }

    @GetMapping("/item/test1")
    public String getItemById(String username,@RequestParam(defaultValue = "100") int age) {
        return "收到的参数是 name:===>" + username + "--age:--->" + age;
    }

    @PostMapping("/item/item")
    public Item getItemById(@RequestBody  Item item) {
        item.setName(item.getName()+"xiugaihoude");
        return item;
    }
    public Item suibianxieyigemingzi(Integer id, HttpServletRequest request) {
        System.err.println("降级的方法执行了");
        System.err.println(count);
        Item item = new Item();
        item.setId(id+1000);
        item.setCategory("降级的分类");
        item.setName("降级的商品的名字");
        item.setPrice(-10000);
        return item;
    }

    @RequestMapping("/name")
    public String getUsername() {
        return username;
    }
}
